﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RPSImporter.Infrastructure;
using System.IO;

namespace RPSImporter.Models.Importers
{
   public class ImporterFactory
   {
      static ImporterFactory instance=null;

      ImporterFactory()
      { }

      public static ImporterFactory Instance
      {
         get
         {
            if (instance == null)
            {
               instance = new ImporterFactory();
            }
            return instance;
         }
      }

      public IImporter GetImporter(string fileName)
      {
         string extension = fileName.Split('.')[1];
         Stream content = File.OpenRead(fileName);

         if (extension == "xls")
         {
            var rpsFile = new RpsFile(FileType.XLS, content, fileName);
            return new XLSImporter(rpsFile);
         }
         else if (extension == "csv")
         {
            var rpsFile = new RpsFile(FileType.CSV, content, fileName);
            return new CSVImporter(rpsFile);
         }
         else if (extension == "xml")
         {
            var rpsFile = new RpsFile(FileType.XML, content, fileName);
            return new XMLImporter(rpsFile);
         }
         return null;
      }
   }
}